package com.cqeec.ui;

import java.awt.Container;
import java.awt.Font;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

import com.cqeec.action.LendingAction;

public class BookRanklListFrame {
	static JInternalFrame bookRanklListFrame = null;
	public BookRanklListFrame() {
	}
	
	public static DefaultPieDataset createDataset() // 创建柱状图数据集
	{
		LendingAction action = new LendingAction();
		String data[][] = action.RanklistTJ();
		DefaultPieDataset dataset=new DefaultPieDataset();
		if (data != null){
			for(int i=0;i<data.length;i++){
				dataset.setValue(data[i][1],Integer.valueOf(data[i][0]));
			}
		}
		// 创建主题样式
		StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
		// 设置标题字体
		standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
		// 设置图例的字体
		standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
		// 设置轴向的字体
		standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
		// 应用主题样式
		ChartFactory.setChartTheme(standardChartTheme);
		return dataset;
	}

	public static JFreeChart createChart(DefaultPieDataset dataset) // 用数据集创建一个图表
	{
		JFreeChart chart = ChartFactory.createPieChart("图书借阅排行榜统计",dataset);
		chart.setTitle(new TextTitle("图书借阅排行榜统计", new Font("宋体", Font.BOLD
				+ Font.ITALIC, 20)));// 可以重新设置标题，替换“hi”标题
		return chart;
	}

	public static JPanel createPanel() {
		JFreeChart chart = createChart(createDataset());
		return new ChartPanel(chart); // 将chart对象放入Panel面板中去，ChartPanel类已继承Jpanel
	}
	
	public static void show(JDesktopPane deskPane) {
		if (bookRanklListFrame == null) {
			bookRanklListFrame = new JInternalFrame("图书借阅排行统计", false, true, false, true);
			bookRanklListFrame.setContentPane(createPanel());
			deskPane.add(bookRanklListFrame);
			final Container c = bookRanklListFrame.getContentPane();
			c.setLayout(null);
			bookRanklListFrame.pack();
			bookRanklListFrame.setVisible(true);
		}
		bookRanklListFrame.addInternalFrameListener(new InternalFrameAdapter() {
			public void internalFrameClosing(InternalFrameEvent e) {
				bookRanklListFrame = null;
			}
		});
	}
}
